package com.cpp.server.aspect;

import com.cpp.common.enums.UserRoleEnum;
import com.cpp.common.exception.BaseException;
import com.cpp.pojo.pojo.User;
import com.cpp.server.aspect.annotation.UserRole;
import com.cpp.server.context.UserHolder;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Slf4j
@Aspect
@Component
@RequiredArgsConstructor
public class UserRoleAspect {
    @SneakyThrows
    @Around(value = "@annotation(judgeRole)") // 现在能正确识别 Log 注解
    public Object log(ProceedingJoinPoint joinPoint, UserRole judgeRole) {
        User currentUser = UserHolder.getCurrentUser();
        UserRoleEnum[] roles = judgeRole.roles();
        String rolesName = "";
        for (UserRoleEnum role : roles) {
            if (currentUser.getRole().equals(role.getRole())) {
                return joinPoint.proceed(); // 继续执行目标方法
            }
            rolesName +="'"+role.getDesc()+"'";
        }
        throw new BaseException("只有"+ rolesName +"才能进行此操作");
    }
}



